package com.syq.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.syq.annotation.RequiresPermissionsDesc;
import com.syq.pojo.DtsAdmin;
import com.syq.service.DtsAdminService;
import com.syq.util.AdminResponseCode;
import com.syq.util.AdminResponseUtil;
import com.syq.util.ResponseUtil;
import com.syq.util.bcrypt.BCryptPasswordEncoder;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName: AdminAdminController
 * @Description: TODO
 * @Create by: syq
 * @Date: 2022/7/25 14:51
 * 管理员管理
 */

@RestController
@RequestMapping("/admin")
@CrossOrigin
public class AdminAdminController {

    @Autowired
    private DtsAdminService dtsAdminService;

    //遍历管理员
    @RequiresPermissionsDesc(menu = {"系统管理","管理员"},button = "查询")
    @RequiresPermissions(value = "admin:admin:list")
    @GetMapping("/admin/list")
    public Object adminList(@RequestParam(defaultValue = "1")Integer page,
                            @RequestParam(defaultValue = "10")Integer limit,
                            @RequestParam(defaultValue = "add_time")String sort,
                            @RequestParam(defaultValue = "desc")String order,String username){


        IPage<DtsAdmin> pageData=dtsAdminService.findPageBy(page,limit,username,sort,order);
        Map<String,Object> map=new HashMap<>();
        map.put("total",pageData.getTotal());
        map.put("items",pageData.getRecords());
        return ResponseUtil.ok(map);
    }

    //更新管理员
    @RequiresPermissionsDesc(menu = {"系统管理","管理员"},button = "编辑")
    @RequiresPermissions(value = "admin:admin:update")
    @PostMapping("/admin/update")
    public Object updateAdmin(@RequestBody DtsAdmin dtsAdmin){

        dtsAdmin.setUpdateTime(new Date());
        dtsAdmin.setPassword(new BCryptPasswordEncoder().encode(dtsAdmin.getPassword()));
        dtsAdminService.updateAdmin(dtsAdmin);
        return ResponseUtil.ok();
    }

    //添加管理员
    @RequiresPermissionsDesc(menu = {"系统管理","管理员"},button = "添加")
    @RequiresPermissions(value = "admin:admin:create")
    @PostMapping("/admin/create")
    public Object createAdmin(@RequestBody DtsAdmin dtsAdmin){
        DtsAdmin dtsAdmin1=dtsAdminService.findByName(dtsAdmin.getUsername());
       if (dtsAdmin1!=null){
           return AdminResponseUtil.fail(AdminResponseCode.ADMIN_NAME_EXIST);
       }
       dtsAdmin.setAddTime(new Date());
       dtsAdmin.setPassword(new BCryptPasswordEncoder().encode(dtsAdmin.getPassword()));
       dtsAdminService.addAdmin(dtsAdmin);
       return ResponseUtil.ok();
    }

    @RequiresPermissionsDesc(menu = {"系统管理","管理员"},button = "删除")
    @RequiresPermissions(value = "admin:admin:delete")
    @GetMapping("/admin/delete")
    public Object deleteAdmin(DtsAdmin dtsAdmin){
        dtsAdminService.delete(dtsAdmin.getId());
        return ResponseUtil.ok();
    }

}
